using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tools.Screens;
using Microsoft.Xna.Framework;

namespace Tools
{
    public static class Extensions
    {
        public static void CleanupComponents(this Game game, TransitionType type)
        {
            TransitionScreen transition = null;
            if (TransitionScreen.first_transition != true)
                transition = new TransitionScreen(game, type);

            for (int i = 0; i < game.Components.Count; i++)
            {
                if (game.Components[i] as IPermanentComponent == null)
                {
                    ((GameComponent)game.Components[i]).Dispose();
                    i--;
                }
            }

            if (TransitionScreen.first_transition != true)
            {
                game.Components.Add(transition);

                transition.OnTransitionStart += () =>
                {
                    for (int i = 0; i < game.Components.Count; i++)
                    {
                        GameComponent gc = (GameComponent)game.Components[i];
                        if (gc.GetType() != typeof(TransitionScreen))
                            gc.Enabled = false;
                    }
                };

                transition.OnTransitionEnd += () =>
                {
                    for (int i = 0; i < game.Components.Count; i++)
                    {
                        GameComponent gc = (GameComponent)game.Components[i];
                        if (gc != transition)
                            gc.Enabled = true;
                    }
                };
            }

            TransitionScreen.first_transition = false;
        }
    }
}
